package com.qiyi.player.wonder.plugins.scenetile.view
{
   import com.iqiyi.components.panelSystem.impls.BasePanel;
   import com.qiyi.player.wonder.common.status.Status;
   import com.qiyi.player.wonder.common.vo.UserInfoVO;
   import common.CommonBg;
   import common.CommonCloseBtn;
   import common.CommonScrollBar;
   import flash.text.TextField;
   import flash.display.Sprite;
   import com.qiyi.player.wonder.plugins.scenetile.SceneTileDef;
   import flash.display.DisplayObjectContainer;
   import com.qiyi.player.wonder.common.utils.StringUtils;
   import com.iqiyi.components.global.GlobalStage;
   import gs.TweenLite;
   import com.qiyi.player.wonder.body.BodyDef;
   import com.qiyi.player.wonder.common.ui.FastCreator;
   import flash.text.TextFieldAutoSize;
   import com.qiyi.player.wonder.common.config.FlashVarConfig;
   import flash.events.MouseEvent;
   import flash.text.TextFormat;
   
   public class SceneTileDetailsView extends BasePanel
   {
      
      public function SceneTileDetailsView(param1:DisplayObjectContainer, param2:Status, param3:UserInfoVO) {
         super(NAME,param1);
         type = BodyDef.VIEW_TYPE_POPUP;
         this._status = param2;
         this._userInfoVO = param3;
         this.initUI();
      }
      
      public static const NAME:String = "com.qiyi.player.wonder.plugins.scenetile.view.SceneTileDetailsView";
      
      private var _status:Status;
      
      private const GAP_LEFT:int = 18;
      
      private const TF_LEADING:int = 7;
      
      private const TF_GAP:int = 23;
      
      private var _isScrollBarMove:Boolean;
      
      private var _userInfoVO:UserInfoVO;
      
      private var _bg:CommonBg;
      
      private var _closeBtn:CommonCloseBtn;
      
      private var _scrollerBar:CommonScrollBar;
      
      private var _tfTitle:TextField;
      
      private var _tfFirstlyTimeAndDirector:TextField;
      
      private var _tfStory:TextField;
      
      private var _tfPerformerVector:Vector.<TextField>;
      
      private var _scrollBarContainer:Sprite;
      
      private var _scrollBarMask:Sprite;
      
      private var _currentScrollV:int = 1;
      
      private var _maxScrollV:uint;
      
      private var _mainActorsNum:uint;
      
      public function onUserInfoChanged(param1:UserInfoVO) : void {
         this._userInfoVO = param1;
      }
      
      public function onAddStatus(param1:int) : void {
         this._status.addStatus(param1);
         switch(param1)
         {
            case SceneTileDef.STATUS_DETAILS_OPEN:
               this.open();
               break;
         }
      }
      
      public function onRemoveStatus(param1:int) : void {
         this._status.removeStatus(param1);
         switch(param1)
         {
            case SceneTileDef.STATUS_DETAILS_OPEN:
               this.close();
               break;
         }
      }
      
      public function onResize(param1:int, param2:int) : void {
         if(isOnStage)
         {
            x = (param1 - this._bg.width) / 2;
            y = (param2 - this._bg.height) / 2;
            if(y < 0)
            {
               y = 0;
            }
         }
      }
      
      override public function open(param1:DisplayObjectContainer = null) : void {
         if(!isOnStage)
         {
            super.open(param1);
            dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_DetailsOpen));
         }
      }
      
      override public function close() : void {
         if(isOnStage)
         {
            super.close();
            dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_DetailsClose));
         }
      }
      
      public function setContent(param1:Object, param2:Object = null) : void {
         var _loc3_:String = null;
         var _loc4_:Array = null;
         var _loc5_:Array = null;
         var _loc6_:uint = 0;
         if(param1 == null)
         {
            return;
         }
         this._tfTitle.text = StringUtils.remainWord(param1.vn,18);
         if((param1.ty) && !(param1.ty == ""))
         {
            this._tfFirstlyTimeAndDirector.text = "上映：" + param1.ty + "年";
         }
         else
         {
            this._tfFirstlyTimeAndDirector.text = "";
         }
         if((param1.d) && !(param1.d == ""))
         {
            _loc3_ = this._tfFirstlyTimeAndDirector.text == ""?"导演：" + param1.d:"     导演：" + param1.d;
            this._tfFirstlyTimeAndDirector.appendText(_loc3_);
         }
         if((param1.info) && !(param1.info == ""))
         {
            this._tfStory.text = param1.info;
         }
         else
         {
            this._tfStory.text = "";
         }
         if(param2)
         {
            _loc4_ = (param2["data"] as Object).mainActors as Array;
            _loc5_ = (param2["data"] as Object).mainActorRoles as Array;
            this._mainActorsNum = _loc4_.length >= _loc5_.length?_loc5_.length:_loc4_.length;
            _loc6_ = 0;
            while(_loc6_ < this._mainActorsNum)
            {
               if(_loc6_ <= 1)
               {
                  this._tfPerformerVector[0].text = this._tfPerformerVector[0].text + (_loc6_ % 2 == 0?this.getPerformerString(_loc4_[_loc6_],_loc5_[_loc6_]):"");
                  this._tfPerformerVector[1].text = this._tfPerformerVector[1].text + (_loc6_ % 2 == 1?this.getPerformerString(_loc4_[_loc6_],_loc5_[_loc6_]):"");
               }
               else
               {
                  this._tfPerformerVector[0].text = this._tfPerformerVector[0].text + (_loc6_ % 2 == 0?"\n" + this.getPerformerString(_loc4_[_loc6_],_loc5_[_loc6_]):"");
                  this._tfPerformerVector[1].text = this._tfPerformerVector[1].text + (_loc6_ % 2 == 1?"\n" + this.getPerformerString(_loc4_[_loc6_],_loc5_[_loc6_]):"");
               }
               _loc6_++;
            }
         }
         this.updateLayout();
         this.onResize(GlobalStage.stage.stageWidth,GlobalStage.stage.stageHeight);
      }
      
      override protected function onAddToStage() : void {
         super.onAddToStage();
         this.onResize(GlobalStage.stage.stageWidth,GlobalStage.stage.stageHeight);
         this.addEventLis();
         alpha = 0;
         TweenLite.to(this,BodyDef.POPUP_TWEEN_TIME / 1000,{"alpha":1});
      }
      
      override protected function onRemoveFromStage() : void {
         super.onRemoveFromStage();
         this._tfTitle.text = "";
         this._tfFirstlyTimeAndDirector.text = "";
         this._tfStory.text = "";
         this._tfPerformerVector[0].text = this._tfPerformerVector[1].text = "";
         this._isScrollBarMove = false;
         this.removeEventLis();
         TweenLite.killTweensOf(this);
      }
      
      private function initUI() : void {
         this._isScrollBarMove = false;
         this._bg = new CommonBg();
         this._bg.width = 480;
         this._bg.height = 300;
         addChild(this._bg);
         this._tfTitle = FastCreator.createLabel("",16777215,14,TextFieldAutoSize.LEFT);
         this._tfTitle.x = this.GAP_LEFT;
         addChild(this._tfTitle);
         this._tfFirstlyTimeAndDirector = FastCreator.createLabel("",16777215,12,TextFieldAutoSize.LEFT);
         if(FlashVarConfig.owner == FlashVarConfig.OWNER_CLIENT)
         {
            this._tfFirstlyTimeAndDirector.cacheAsBitmap = true;
         }
         this._tfFirstlyTimeAndDirector.x = this.GAP_LEFT;
         addChild(this._tfFirstlyTimeAndDirector);
         this._closeBtn = new CommonCloseBtn();
         this._closeBtn.x = this._bg.width - this._closeBtn.width - 5;
         this._closeBtn.y = 1;
         addChild(this._closeBtn);
         this._closeBtn.addEventListener(MouseEvent.CLICK,this.onCloseBtnClick);
         this.initScrollBar();
      }
      
      private function initScrollBar() : void {
         var _loc2_:TextField = null;
         this._scrollBarContainer = new Sprite();
         this._scrollBarContainer.x = this.GAP_LEFT;
         addChild(this._scrollBarContainer);
         this._tfStory = FastCreator.createLabel("",16777215,12,TextFieldAutoSize.LEFT);
         var _loc1_:TextFormat = new TextFormat();
         _loc1_.leading = this.TF_LEADING;
         this._tfStory.defaultTextFormat = _loc1_;
         this._tfStory.mouseEnabled = false;
         this._tfStory.selectable = false;
         this._tfStory.wordWrap = true;
         this._tfStory.multiline = true;
         this._tfStory.textColor = 16777215;
         this._tfStory.width = 442;
         this._scrollBarContainer.addChild(this._tfStory);
         this._tfPerformerVector = new Vector.<TextField>();
         var _loc3_:uint = 0;
         while(_loc3_ < 2)
         {
            _loc2_ = FastCreator.createLabel("",16777215,12,TextFieldAutoSize.LEFT);
            _loc2_.defaultTextFormat = _loc1_;
            _loc2_.mouseEnabled = false;
            _loc2_.selectable = false;
            _loc2_.wordWrap = true;
            _loc2_.textColor = 16777215;
            _loc2_.width = 220;
            _loc2_.x = _loc3_ * _loc2_.width;
            this._scrollBarContainer.addChild(_loc2_);
            this._tfPerformerVector.push(_loc2_);
            _loc3_++;
         }
         this._scrollBarMask = new Sprite();
         this._scrollBarMask.graphics.beginFill(65280);
         this._scrollBarMask.graphics.drawRect(this.GAP_LEFT,0,442,200);
         this._scrollBarMask.graphics.endFill();
         addChild(this._scrollBarMask);
         this._scrollBarContainer.mask = this._scrollBarMask;
         this._scrollerBar = new CommonScrollBar();
         this._scrollerBar.x = this._tfStory.width + this.GAP_LEFT + 3;
         addChild(this._scrollerBar);
      }
      
      private function addEventLis() : void {
         this._scrollerBar.addEventListener(MouseEvent.MOUSE_DOWN,this.onScrollerBarMouseDown);
         GlobalStage.stage.addEventListener(MouseEvent.MOUSE_MOVE,this.onStageMouseMove);
         GlobalStage.stage.addEventListener(MouseEvent.MOUSE_UP,this.onStageMouseUp);
      }
      
      private function removeEventLis() : void {
         this._scrollerBar.removeEventListener(MouseEvent.MOUSE_DOWN,this.onScrollerBarMouseDown);
         GlobalStage.stage.removeEventListener(MouseEvent.MOUSE_MOVE,this.onStageMouseMove);
         GlobalStage.stage.removeEventListener(MouseEvent.MOUSE_UP,this.onStageMouseUp);
      }
      
      private function updateLayout() : void {
         var _loc1_:int = this.TF_GAP;
         this._tfTitle.y = _loc1_;
         _loc1_ = _loc1_ + (this._tfTitle.textHeight + this.TF_LEADING * 2);
         if(this._tfFirstlyTimeAndDirector.text != "")
         {
            this._tfFirstlyTimeAndDirector.y = _loc1_;
            _loc1_ = _loc1_ + (this._tfFirstlyTimeAndDirector.textHeight + this.TF_LEADING);
         }
         if(this._tfStory.text != "")
         {
            _loc1_ = _loc1_ + (this._tfFirstlyTimeAndDirector.text == ""?0:this.TF_LEADING);
            this._scrollBarContainer.y = this._scrollBarMask.y = this._scrollerBar.y = _loc1_;
            this._tfStory.y = 0;
            _loc1_ = _loc1_ + this._tfStory.textHeight;
         }
         var _loc2_:Number = this._tfPerformerVector[0].textHeight + this._tfStory.textHeight + this.TF_GAP;
         var _loc3_:Number = _loc2_ > this._scrollBarMask.height?_loc2_:this._scrollBarMask.height;
         this._scrollBarContainer.graphics.clear();
         this._scrollBarContainer.graphics.beginFill(16776960,0);
         this._scrollBarContainer.graphics.drawRect(0,0,this._scrollBarContainer.width,_loc3_);
         this._scrollBarContainer.graphics.endFill();
         this._maxScrollV = Math.ceil((this._tfStory.height - this._scrollBarMask.height) / this.TF_GAP) + Math.ceil(this._mainActorsNum / 2) + 1;
         this._tfPerformerVector[0].y = this._tfPerformerVector[1].y = this._tfStory.y + this._tfStory.height + this.TF_LEADING * 2;
         this._scrollerBar.visible = _loc2_ > this._scrollBarMask.height?true:false;
         this._scrollerBar.height = this._scrollBarMask.height / this._scrollBarContainer.height * this._scrollBarMask.height;
      }
      
      private function onContainerScrollChange(param1:MouseEvent) : void {
         this._currentScrollV = this._currentScrollV - param1.delta;
         this._currentScrollV = this._currentScrollV <= 1?1:this._currentScrollV;
         this._currentScrollV = this._currentScrollV > this._maxScrollV?this._maxScrollV:this._currentScrollV;
         this._scrollerBar.y = (this._currentScrollV - 1) / this._maxScrollV * (this._scrollBarMask.height - this._scrollerBar.height) + this._scrollBarMask.y;
         this.updataScrollBarContainer();
      }
      
      private function onStageMouseMove(param1:MouseEvent) : void {
         if(this._isScrollBarMove == true)
         {
            this._scrollerBar.y = mouseY - this._scrollerBar.height / 2;
         }
         if(this._scrollerBar.y >= this._scrollBarMask.y + this._scrollBarMask.height - this._scrollerBar.height)
         {
            this._scrollerBar.y = this._scrollBarMask.y + this._scrollBarMask.height - this._scrollerBar.height;
         }
         else if(this._scrollerBar.y <= this._scrollBarMask.y)
         {
            this._scrollerBar.y = this._scrollBarMask.y;
         }
         
         this._currentScrollV = Math.floor((this._scrollerBar.y - this._scrollBarMask.y) / (this._scrollBarMask.height - this._scrollerBar.height) * this._maxScrollV);
         this.updataScrollBarContainer();
      }
      
      private function updataScrollBarContainer() : void {
         var _loc1_:Number = this._tfPerformerVector[0].textHeight + this._tfStory.textHeight + this.TF_GAP;
         var _loc2_:Number = _loc1_ > this._scrollBarMask.height?_loc1_:this._scrollBarMask.height;
         var _loc3_:Number = -(_loc2_ - this._scrollBarMask.height - this._scrollBarMask.y);
         this._scrollBarContainer.y = -(this._currentScrollV - 1) / this._maxScrollV * this._scrollBarContainer.height + this._scrollBarMask.y;
         this._scrollBarContainer.y = this._scrollBarContainer.y > this._scrollBarMask.y?this._scrollBarMask.y:this._scrollBarContainer.y;
         this._scrollBarContainer.y = this._scrollBarContainer.y < _loc3_?_loc3_:this._scrollBarContainer.y;
      }
      
      private function onStageMouseUp(param1:MouseEvent) : void {
         this._isScrollBarMove = false;
      }
      
      private function onScrollerBarMouseDown(param1:MouseEvent) : void {
         this._isScrollBarMove = true;
      }
      
      private function getPerformerString(param1:String, param2:String) : String {
         var _loc3_:String = null;
         _loc3_ = param1.length < 3?param1 + "  ":param1;
         _loc3_ = _loc3_ + ("  饰演  " + param2);
         return StringUtils.remainWord(_loc3_,22);
      }
      
      private function onCloseBtnClick(param1:MouseEvent) : void {
         this.close();
      }
   }
}
